Skip to content

[中文版本](/posts/c教程/zh-CN/02-语言基础/013-Operators and Expressions)

Operators are the building blocks of computation in C. They let you perform arithmetic, compare values, manipulate bits, and combine logic, all in concise expressions. Once you understand how operators work and how they interact through precedence and associativity, you can write clear, efficient code that behaves exactly as you expect.

Tiny Code ​

#include <stdio.h>
int main(void) {
    int a = 10, b = 3;
    printf("a + b = %d\n", a + b);   // addition
    printf("a - b = %d\n", a - b);   // subtraction
    printf("a * b = %d\n", a * b);   // multiplication
    printf("a / b = %d\n", a / b);   // integer division
    printf("a %% b = %d\n", a % b);  // remainder (modulo)
    a += 5; // same as a = a + 5
    printf("a after += 5: %d\n", a);
    return 0;
}

Output:

a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1
a after += 5: 15

Arithmetic Operators ​

OperatorMeaningExampleResult
+Addition4 + 37
-Subtraction10 - 64
*Multiplication2 * 510
/Division7 / 23 (integer division)
%Modulo (remainder)7 % 21

Tip: If you use floating-point numbers (float,double), division produces decimals.

Relational and Logical Operators ​

TypeOperatorExampleMeaning
Relational==a == bEqual
!=a != bNot equal
<,>,<=,>=a < bComparison
Logical&&a && bLogical AND
```a
!!aLogical NOT

Example:

int age = 20;
if (age >= 18 && age <= 60)
    printf("Adult\n");

Increment and Decrement ​

int x = 5;
printf("%d\n", ++x); // prefix: increments, then uses value (6)
printf("%d\n", x++); // postfix: uses value, then increments (6)
printf("%d\n", x);   // final value is 7

Assignment and Compound Operators ​

OperatorMeaningExample
=Assignmentx = 10
+=Add and assignx += 2
-=Subtract and assignx -= 3
*=Multiply and assignx *= 4
/=Divide and assignx /= 5
%=Modulo and assignx %= 6

These save typing and make intent clearer.

Bitwise Operators ​

C gives you direct access to bits, useful for systems, embedded, or optimization tasks.

OperatorMeaningExample
&ANDa & b
``OR
^XORa ^ b
~NOT~a
<<Left shifta << 1
>>Right shifta >> 2

Example:

int mask = 0b0010;
int num = 0b1011;
int result = num & mask;  // checks if the 2nd bit is set

Precedence and Associativity ​

When you write complex expressions, C follows operator precedence rules. For example:

int result = 2 + 3 * 4; // result is 14, not 20

* has higher precedence than+, so it runs first.

Use parentheses to make intentions clear:

int result = (2 + 3) * 4; // result is 20

Why It Matters ​

Operators are where logic meets the machine. They translate mathematical ideas and control decisions into instructions the CPU executes directly. Understanding how expressions are built and evaluated helps you:

  • Write compact, efficient code
  • Avoid precedence mistakes
  • Control exactly what your program computes

In low-level work (like bitwise operations or embedded systems), operator mastery is essential.

Try It Yourself ​

Write a small program that takes two integers and prints their:

  • Sum
  • Difference
  • Product
  • Quotient
  • Remainder

Modify it to print results as floating-point values.

Use logical operators to test if both numbers are positive.

Try combining bitwise operations:

printf("%d\n", 5 & 3);  // AND
printf("%d\n", 5 | 3);  // OR
printf("%d\n", 5 ^ 3);  // XOR

Experiment with parentheses and operator order until you can predict every result.

Operators are where C’s simplicity meets its power, a small set of symbols that give you total control over computation, logic, and even raw bits.

本站总访问 — 次